package com.example.myhotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myhotel.common.R;
import com.example.myhotel.entity.Book;
import com.example.myhotel.entity.Rooms;
import com.example.myhotel.service.BillService;
import com.example.myhotel.service.BookService;
import com.example.myhotel.service.RoomsService;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("room")
@CrossOrigin
public class RoomsController {
    @Autowired
    private RoomsService roomsService;
    @Autowired
    private BookService bookService;
    @Autowired
    private BillService billService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @GetMapping ("/page")
    public R<Page> page( int page,int pageSize,int state,String roomType,String address,String prices,String id){//state为顾客显示还能预定的房间
        //构建分页构造器
        Page<Rooms> roomsPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Rooms> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(state == 0,Rooms::getCount,state);
        wrapper.like(roomType != null,Rooms::getRoomType,roomType);//房间型号模糊查询
        wrapper.like(address != null,Rooms::getAddress,address);//房间号模糊查询
        wrapper.like(id != null,Rooms::getId,id);//完整id查询
        if (prices != null && prices.equals("order")){
            wrapper.orderByAsc(Rooms::getPrices);
        }
        roomsService.page(roomsPage,wrapper);
        return R.success(roomsPage);
    }
    @GetMapping("list")
    public List<Rooms> list(){
        List<Rooms> list = roomsService.list();
//        System.out.println(list);
        return list;
    }
    @PostMapping("/setRoom")
//    @RequestMapping("/setRoom")
    public R<String> setImage(@RequestBody Rooms rooms){
        /**/
        if (roomsService.chekSame(rooms)){
            rooms.setCount(1);
            roomsService.save(rooms);
            return R.success("添加成功");
        }
        return R.error("已有相同房间号");

    }
    @DeleteMapping
    public R<String> delete(String id){

        boolean chekBook = bookService.chekBook(null, id);
        if (chekBook){
            roomsService.removeById(id);
            //删除时将预定也删了
            bookService.deleteBook(null,id);
            return R.success("成功删除");
        }
        return R.error("该房间已被用户预定");

    }
    @PutMapping
    public R<String> update(@RequestBody Rooms rooms){
        roomsService.updateById(rooms);
        return R.success("成功修改信息");
    }


}
